home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October : Technology Seed / ATS Oct. '97.toast / Navigation Services SDK 1.0a6 / Examples / SimpleText / SimpleText ƒ / NavigationServicesSupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-31  |  7.3 KB  |  335 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NavigationServicesSupport.c
  3.  
  4.     Contains:    <contents>
  5.  
  6.     Written by:    Sean Findley
  7.  
  8.     Copyright:    <copyright>
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <19>    10/20/97    KLM        more a6 interface changes
  13.         <18>    10/13/97    KLM        a6 interface changes
  14.         <17>    /19/1997    Yan        Mixed mode support
  15.         <15>     5/23/97    sjf     
  16. */
  17.  
  18.  
  19. /*
  20.     File:        NavigationServicesSupport.c
  21.  
  22.     Contains:    xxx put contents here xxx
  23.  
  24.     Version:    xxx put the technology version here xxx
  25.  
  26.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  27.  
  28.     File Ownership:
  29.  
  30.         DRI:                Yan Arrouye
  31.  
  32.         Other Contact:        xxx put alternate contact (owner’s functional manager) here xxx
  33.  
  34.         Technology:            xxx put the technology group name here xxx
  35.  
  36.     Writers:
  37.  
  38.         (KLM)    Keith Mortensen
  39.         (Yan)    Yan Arrouye
  40.  
  41.     Change History (most recent first):
  42.  
  43.         <14>     5/15/97    Yan        Updated for new API
  44.         <13>     5/08/97    KLM        Fixed NewOpenHandle bug, it was never creating a open rsrc.
  45.         <12>     5/08/97    Yan        Use isStationery flag
  46.         <11>     4/21/97    Yan        
  47.         <10>     4/21/97    Yan        NavGetDefaultDialogsOptions really
  48.          <9>     4/21/97    Yan        Use new NavSetDefaultDialogsOptions
  49.          <8>     4/21/97    Yan        Implemented isStationery in save
  50.          <7>     4/21/97    Yan        Updated for latest include
  51.          <6>     4/18/97    Yan        Rev'ed for new API
  52.          <5>     4/14/97    Yan        Added a NULL version of this file for classic 68K runtime (not
  53.                                     supported)
  54. */
  55.  
  56. #include "NavigationServicesSupport.h"
  57.  
  58.  
  59.  
  60. #include <CodeFragments.h>
  61. #include <Finder.h>
  62. #include <Dialogs.h>
  63. #include <LowMem.h>
  64. #include <string.h>
  65.  
  66.  
  67.  
  68.  
  69.  
  70. static Handle NewOpenHandle(OSType applicationSignature, short numTypes, OSType typeList[])
  71. {
  72.     Handle hdl = NULL;
  73.     
  74.     if ( numTypes > 0 )
  75.     {
  76.     
  77.         hdl = NewHandle(sizeof(NavTypeList) + numTypes * sizeof(OSType));
  78.     
  79.         if ( hdl != NULL )
  80.         {
  81.             NavTypeListHandle open        = (NavTypeListHandle)hdl;
  82.             
  83.             (*open)->componentSignature = applicationSignature;
  84.             (*open)->osTypeCount        = numTypes;
  85.             BlockMoveData(typeList, (*open)->osType, numTypes * sizeof(OSType));
  86.         }
  87.     }
  88.     
  89.     return hdl;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. static OSStatus SendOpenAE(AEDescList list)
  96. {
  97.     OSStatus        err;
  98.     AEAddressDesc    theAddress;
  99.     AppleEvent        dummyReply;
  100.     AppleEvent        theEvent;
  101.     
  102.     theAddress.descriptorType    = typeNull;
  103.     theAddress.dataHandle        = NULL;
  104.  
  105.     do {
  106.         ProcessSerialNumber psn;
  107.     
  108.         err = GetCurrentProcess(&psn);
  109.         if ( err != noErr) break;
  110.         
  111.         err =AECreateDesc(typeProcessSerialNumber, &psn, sizeof(ProcessSerialNumber), &theAddress);
  112.         if ( err != noErr) break;
  113.             
  114.         dummyReply.descriptorType    = typeNull;
  115.         dummyReply.dataHandle        = NULL;
  116.  
  117.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  118.         if ( err != noErr) break;
  119.         
  120.         err = AEPutParamDesc(&theEvent, keyDirectObject, &list);
  121.         if ( err != noErr) break;
  122.         
  123.         err = AESend(&theEvent, &dummyReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  124.         if ( err != noErr) break;
  125.         
  126.             
  127.     } while (false);
  128.     
  129.     return err;
  130. }
  131.  
  132.  
  133.  
  134. OSStatus OpenFileDialog(OSType applicationSignature, short numTypes, OSType typeList[], NavEventProcPtr eventProc, FSSpec* fileSpec, OSType* fileType)
  135. {
  136.     NavReplyRecord        theReply;
  137.     NavDialogOptions    dialogOptions;
  138.     OSErr                theErr        = noErr;
  139.     NavTypeListHandle    openList    = NULL;
  140.     NavEventUPP            eventUPP    = NewNavEventProc(eventProc);
  141.  
  142.     NavGetDefaultDialogOptions(&dialogOptions);
  143.  
  144.     openList = (NavTypeListHandle)NewOpenHandle(applicationSignature, numTypes, typeList);
  145.     if ( openList )
  146.     {
  147.         HLock((Handle)openList);
  148.     }
  149.     
  150.     theErr = NavGetFile(NULL, &theReply, &dialogOptions, eventUPP, NULL, NULL, openList, NULL);
  151.     DisposeRoutineDescriptor(eventUPP);
  152.     
  153.     if (theReply.validRecord)
  154.     {
  155.         // grab the target FSSpec from the AEDesc for opening:    
  156.         AEDesc     resultDesc;
  157.         FInfo    fileInfo;
  158.  
  159.         // Is this a single file open
  160.         if ( fileSpec != NULL )
  161.         {
  162.             AEKeyword keyword;
  163.             
  164.             theErr = AEGetNthDesc(&theReply.selection, 1, typeFSS, &keyword, &resultDesc);
  165.             if (theErr == noErr)
  166.                 BlockMove(*resultDesc.dataHandle,fileSpec,sizeof(FSSpec));
  167.                 
  168.             // decide if the doc we fileSpec opening is a PICT or TEXT
  169.             theErr = FSpGetFInfo(fileSpec, &fileInfo);
  170.             if (theErr == noErr)
  171.             {
  172.                 if ( fileType != NULL )
  173.                     *fileType = fileInfo.fdType;
  174.             }
  175.         }
  176.         else
  177.         {
  178.             // Multiple files open: use ApleEvents
  179.             theErr = SendOpenAE(theReply.selection);
  180.         }
  181.  
  182.         NavDisposeReply(&theReply);
  183.     }
  184.     else
  185.     {
  186.         theErr = userCanceledErr;
  187.     }
  188.  
  189.     if (openList != NULL)
  190.     {
  191.         HUnlock((Handle)openList);
  192.         DisposeHandle((Handle)openList);
  193.     }
  194.     
  195.     return theErr;
  196. }
  197.  
  198.  
  199. short ConfirmSaveDialog(StringPtr documentName, Boolean quitting, NavEventProcPtr eventProc)
  200. {
  201.     OSStatus                theStatusErr     = noErr;
  202.     OSErr                     theErr             = noErr;
  203.     NavAskSaveChangesResult    reply             = 0;
  204.     NavAskSaveChangesAction    action             = 0;
  205.     NavEventUPP                eventUPP        = NewNavEventProc(eventProc);
  206.     short                    result;
  207.     
  208.     if (quitting)
  209.         action = kNavSaveChangesQuittingApplication;
  210.     else
  211.         action = kNavSaveChangesClosingDocument;
  212.         
  213.     theErr = NavAskSaveChanges(    LMGetCurApName(),
  214.                                 documentName,
  215.                                 action,
  216.                                 &reply,
  217.                                 eventUPP,
  218.                                 NULL);
  219.     DisposeRoutineDescriptor(eventUPP);
  220.     
  221.     // Map reply code to ok, cancel, dontSave
  222.     switch (reply)
  223.     {
  224.         case askSaveChangesSave:
  225.             result = ok;
  226.             break;
  227.             
  228.         case askSaveChangesCancel:
  229.             result = cancel;
  230.             break;
  231.             
  232.         case askSaveChangesDontSave:
  233.             result = dontSaveChanges;
  234.             break;
  235.     }
  236.     
  237.     return result;
  238. }
  239.  
  240.  
  241.  
  242. OSStatus SaveFileDialog(StringPtr fileName, OSType filetype, OSType fileCreator, 
  243.                         NavEventProcPtr eventProc, FSSpec* fileSpec, 
  244.                         Boolean* stationery, Boolean* replacing, NavReplyRecord* reply)
  245. {
  246.     NavDialogOptions    dialogOptions;
  247.     OSErr                theErr        = noErr;
  248.     NavEventUPP            eventUPP    = NewNavEventProc(eventProc);
  249.  
  250.     NavGetDefaultDialogOptions(&dialogOptions);
  251.  
  252.     dialogOptions.dialogOptionFlags |= (stationery != NULL ? kAllowStationery : 0);
  253.     BlockMoveData(fileName, (unsigned char*)&dialogOptions.savedFileName, fileName[0] + 1);
  254.  
  255.     theErr = NavPutFile(NULL, reply, &dialogOptions, eventUPP, filetype, fileCreator, NULL);
  256.     DisposeRoutineDescriptor(eventUPP);
  257.     
  258.     if (reply->validRecord)
  259.     {
  260.         // User saved
  261.         AEDesc     resultDesc;
  262.         AEKeyword keyword;
  263.             
  264.         // retrieve the returned selection:
  265.         theErr = AEGetNthDesc(&reply->selection, 1, typeFSS, &keyword, &resultDesc);
  266.         if (theErr == noErr)
  267.             BlockMove(*resultDesc.dataHandle, fileSpec, sizeof(FSSpec));
  268.  
  269.         if ( replacing != NULL )
  270.             *replacing = reply->replacing;
  271.         
  272.         if ( stationery != NULL )
  273.         {
  274.             *stationery    = reply->isStationery;
  275.         }
  276.     }
  277.     else
  278.     {
  279.         // User cancelled
  280.         if ( replacing != NULL )
  281.             *replacing = false;
  282.         
  283.         if ( stationery != NULL )
  284.             *stationery    = false;    
  285.  
  286.         theErr = userCanceledErr;
  287.     }
  288.     
  289.     return theErr;
  290. }
  291.  
  292.  
  293.  
  294. OSStatus CompleteSave(const FSSpec*, NavReplyRecord* reply)
  295. {
  296.     OSStatus theErr;
  297.     
  298.     if (reply->validRecord)
  299.     {
  300.         theErr = NavCompleteSave(reply, kNavTranslateInPlace);
  301.     }
  302.  
  303.     theErr = NavDisposeReply(reply);
  304.     
  305.     return theErr;
  306. }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314. //
  315. // Callback to handle events that occur while navigation dialogs are up but really should be handled by the application
  316. //
  317.  
  318. extern void HandleEvent(EventRecord * pEvent);
  319.  
  320.  
  321. pascal void MyEventProc(const NavEventCallbackMessage callBackSelector, 
  322.                         NavCBRecPtr callBackParms, 
  323.                         NavCallBackUserData /*callBackUD*/)
  324. // Callback to handle event passing betwwn the navigation dialogs and the applicatio
  325. {
  326.     if ( callBackSelector == kNavCBEvent )
  327.         switch (callBackParms->eventData.event->what)
  328.         {
  329.             case updateEvt:
  330.                 HandleEvent(callBackParms->eventData.event);
  331.                 break;
  332.         }
  333. }
  334.  
  335.